package me.youline.dataServer.service.pubMsg;

import java.io.File;
import java.util.List;

import me.youline.dataServer.dao.ArContentEntityDAO;
import me.youline.dataServer.dao.MsgPraiseRecordDAO;
import me.youline.dataServer.dao.PubMsgDAO;
import me.youline.dataServer.dao.PubMsgIndexDAO;
import me.youline.dataServer.dao.PubMsgStatisticRecorderDAO;
import me.youline.dataServer.entity.ArContentEntity;
import me.youline.dataServer.entity.PublicMessage;
import me.youline.dataServer.exception.UnExistDataException;
import me.youline.dataServer.exception.UnPermitAccessException;
import me.youline.dataServer.service.MsgPraiseRecordService;
import me.youline.dataServer.service.SocialActionNotifyMsgService;
import me.youline.dataServer.service.support.file.FileLocatorManager;

import org.apache.commons.lang3.StringUtils;
import org.perf4j.aop.Profiled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

@Component
public class PubMsgUpdateService {

	@Autowired
	private PubMsgDAO publicMessageDAO;

	@Autowired
	private PubMsgIndexDAO publicMessageIndexDAO;

	@Autowired
	private ArContentEntityDAO arContentDAO;

	@Autowired
	private MsgPraiseRecordDAO msgPraiseRecordDAO;

	@Autowired
	private FileLocatorManager locatorManager;

	@Autowired
	private MsgPraiseRecordService msgPraiseRecordService;

	@Autowired
	private SocialActionNotifyMsgService socialActionNotifyMsgService;

	@Autowired
	private PubMsgStatisticRecorderDAO msgRecorderDAO;
	
	@Autowired
	private MsgPraiseRecordService praiseRecordService;
	
	/**
	 * 删除公告消息
	 * 
	 * @param dto
	 * @throws UnExistDataException
	 * @throws UnPermitAccessException
	 */
	@Transactional(rollbackFor = Exception.class)
	@Profiled(tag = "service-删除消息")
	public void deletePublicMessage(Long messageId, Long processUserId)
			throws UnExistDataException{
		PublicMessage msg = publicMessageDAO.findOne(messageId);
		if(msg == null){
			throw new UnExistDataException("删除的公告已经不存在了");
		}
		this.doPubMsgDataClear(msg);
		this.doPubMsgImgResourceClear(msg);
	}
	
	/**
	 * 清除文件
	 * @param entity
	 */
	protected void doPubMsgContentFileClear(ArContentEntity entity) {
		if (entity != null) {
			arContentDAO.delete(entity);
			String url = locatorManager.getContentFileLocator().getFilePath(entity.getContentUrl());
			File contentFile = new File(url);
			contentFile.delete();
		}
	}
	
	
	/**
	 * 执行公告相关数据的清除
	 * @param msg
	 */
	protected void doPubMsgDataClear(PublicMessage msg){
		msgRecorderDAO.delete(msg.getId());// 删除消息统计实体
		msgPraiseRecordDAO.deletePraiseRecordByMsgId(msg.getId());// 删除原创消息点赞记录
		publicMessageDAO.delete(msg.getId());// 删除消息
		publicMessageIndexDAO.deleteMsgIndex(Lists// 删除消息索引
				.newArrayList(msg.getId()));
		socialActionNotifyMsgService.deleteNotifyMsgByOrigin(msg.getId());// 删除所有关于原创的通知消息。
		
	}
	
	/**
	 * 执行公告相关文件资源的清除，当前包括识别图、效果图
	 * @param msg
	 */
	private void doPubMsgImgResourceClear(PublicMessage msg){
		this.deleteDisplayImageFile(msg.getDisplayPicUrl());
		this.deleteRecognImageFile(msg.getRecognizePicUrl());
	}
	
	/**
	 * 根据公告相关图片url片段删除识别图片文件
	 * @param url
	 */
	private void deleteRecognImageFile(String url){
		if(StringUtils.isBlank(url)){
			return;
		}
		List<File> list = locatorManager.getRecognPicFileLocator()
				.getAllRelatedSizeFiles(url);
		for (File f : list) {
			f.delete();
		}
	}
	
	/**
	 * 根据公告相关图片url片段删除效果展示图片文件
	 * @param url
	 */
	private void deleteDisplayImageFile(String url){
		if(StringUtils.isBlank(url)){
			return;
		}
		List<File> list = locatorManager.getDisplayPicFileLocator()
				.getAllRelatedSizeFiles(url);
		for (File f : list) {
			f.delete();
		}
	}
	
}
